package com.zzb.servlet.product;


import com.alibaba.fastjson.JSONObject;
import com.zzb.DTO.ResultDTO;
import com.zzb.VO.ProductVO;
import com.zzb.service.ProductService;
import com.zzb.service.impl.ProductServiceImpl;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.nio.charset.StandardCharsets;


@WebServlet("/back/product/delete")
public class ProductDeleteServlet extends HttpServlet {
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws  IOException {
        //乱码处理
        req.setCharacterEncoding(StandardCharsets.UTF_8.toString());
        resp.setCharacterEncoding(StandardCharsets.UTF_8.toString());

        //得到前端数据
        String id = req.getParameter("id");

        //对前端数据进行封装
        ProductVO productVO = ProductVO.builder()
                .id(Integer.valueOf(id))
                .build();

        //调用服务逻辑
        ProductService productService = new ProductServiceImpl();
        //得到响应数据
        ResultDTO resultDTO = productService.delete(productVO);
        //将数据响应给前端
        resp.getWriter().println(JSONObject.toJSONString(resultDTO));
    }
}
